let db = require("../../../db/db")

let handle = async function (action, data, login_info, req, res) {
	let user_id = login_info.user_id;
	let result,rows,optins;
	let resourceName;
	switch (action) {
		case "resources": 
			let c_sql = "SELECT distinct mr.code as label, mr.name as value FROM mes_resource mr \
            left join mes_employee_role  emr on emr.resource_id=mr.id \
            left join sys_user su on su.emp_id= emr.employee_id \
            where su.id=?"; 
			let sql = c_sql;
		    result = await db.query(sql, [user_id]);
            rows = result.rows;
            options = [];
			for (let row of rows) {
				options.push(row.label);
			}
			return (options);	
			case "process":
				resourceName=data.related_resource;
				let c_sql1 = "SELECT distinct mp.code as label, mp.name as value FROM mes_process mp \
				left join mes_employee_role  emr on emr.process_id=mp.id \
				left join mes_resource mr on mr.id= emr.resource_id \
				left join sys_user su on su.emp_id= emr.employee_id \
				where su.id=? and mr.code=?" ; 
				let sql1 = c_sql1;
				 result = await db.query(sql1, [user_id,resourceName]);
				 rows = result.rows;
				 options = [];
				for (let row of rows) {
					options.push(row.label);
				}
				return (options);	
				case "station":
					resourceName=data.related_resource;
					let processName=data.related_process;
					let c_sql2 = "select distinct s.code as label,s.name as value from mes_station s\
					left join mes_resource mr on mr.id=s.resource_id\
					left join mes_process mp on mp.id=s.process_id\
					where mr.code=? and mp.code=?" ; 
					let sql2 = c_sql2;
					 result = await db.query(sql2, [resourceName,processName]);
					 rows = result.rows;
					 options = [];
					for (let row of rows) {
						options.push(row.label);
					}
					return (options);	
					case "sn":
						return ({ result: "OK1:"+data.text });

				default:
			return Error('未知的action:' + action);
	}
}

module.exports = { handle };